package com.tedu.jiaodanote.controller;


import com.tedu.jiaodanote.commen.CommonResult;
import com.tedu.jiaodanote.entry.Note;
import com.tedu.jiaodanote.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author：毕龙
 * Date: 2024/7/8 14:39
 */
@Api(tags = "读书杂记")
@RestController
@RequestMapping("/note")
public class NoteController {
    @Autowired
    private NoteService noteService;

    @ApiOperation(value = "查询杂记信息")
    @GetMapping()
    public CommonResult list() {
        return CommonResult.success(noteService.listNote());
    }

    @ApiOperation("添加杂记信息")
    @PostMapping
    public CommonResult save(@RequestBody Note note) {
        System.out.println(note);
        note.setCtime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        return CommonResult.success(noteService.save(note));
    }

    @ApiOperation("修改杂记信息")
    @PutMapping
    public CommonResult update(@RequestBody Note note) {
        note.setCtime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        return CommonResult.success(noteService.updateById(note));
    }

    @ApiOperation("删除杂记信息")
    @DeleteMapping
    public CommonResult remove(Long noteid) {
        return CommonResult.success(noteService.removeById(noteid));
    }
}
